package net.stogi.dsper.ui.client.module.datasource;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.dispatch.client.core.ActionManager;
import net.stogi.common.gwt.frame.client.core.EventBus;
import net.stogi.common.gwt.frame.client.mvp.AbstractPresenter;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.common.mvp.core.model.KeyValues;
import net.stogi.dsper.api.dto.DataSourceDto;
import net.stogi.dsper.ui.shared.module.datasource.ListDataSourcesAction;
import net.stogi.dsper.ui.shared.module.datasource.ListDataSourcesResult;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

class DataSourceSelectorPresenter extends AbstractPresenter {

	private DataSourceSelectorView dataSourceSelectorView;

	private List<DataSourceDto> dataSourceListModel;

	public DataSourceSelectorPresenter(DataSourceSelectorView dataSourceSelectorView, ActionManager actionManager, EventBus eventBus) {
		super(actionManager, eventBus);
		this.dataSourceSelectorView = dataSourceSelectorView;
		initBehavior();
		initHandlers();
	}

	@Override
	protected void initBehavior() {
		dataSourceSelectorView.setSelectBehavior(new BehaviorWithValue<List<KeyValues<Long>>>() {

			@Override
			public void behave(List<KeyValues<Long>> values) {
				List<DataSourceDto> selectedDataSourceList = new ArrayList<DataSourceDto>();
				for (KeyValues<Long> value : values) {
					DataSourceDto selectedDataSource = internalGetDataSourceFromModel(value.getKey());
					if (selectedDataSource != null) {
						selectedDataSourceList.add(selectedDataSource);
					}
				}

				GWT.log("DataSourceSelectorPresenter.behavior.select: [selectedDataSourceList.size = '" + selectedDataSourceList.size()
						+ "']", null);

				dataSourceSelectorView.disappear();
				eventBus.fireEvent(new DataSourcesSelectedEvent(selectedDataSourceList));
			}
		});
		dataSourceSelectorView.setCancelBehavior(new Behavior() {

			@Override
			public void behave() {
				dataSourceSelectorView.disappear();
				eventBus.fireEvent(new DataSourceSelectorClosedEvent());
			}
		});
	}

	@Override
	protected void initHandlers() {
		eventBus.addHandler(OpenDataSourceSelectorEvent.TYPE, new OpenDataSourceSelectorHandler() {

			@Override
			public void onOpenDataSourceSelector(OpenDataSourceSelectorEvent event) {
				dataSourceSelectorView.appear();
				executeListDataSources();
			}
		});
	}

	private void executeListDataSources() {
		actionManager.execute(new ListDataSourcesAction(), new AsyncCallback<ListDataSourcesResult>() {

			@Override
			public void onFailure(Throwable caught) {
				internalHandleException(caught);
			}

			@Override
			public void onSuccess(ListDataSourcesResult result) {
				dataSourceListModel = result.getDataSources();
				internalDisplayDataSources(dataSourceListModel);
			}
		});
	}

	private void internalDisplayDataSources(List<DataSourceDto> dataSourceList) {
		List<KeyValues<Long>> viewModelList = new ArrayList<KeyValues<Long>>();
		for (DataSourceDto dataSource : dataSourceList) {
			KeyValues<Long> viewModel = new KeyValues<Long>(dataSource.getId());
			viewModel.setValue("name", dataSource.getName());
			viewModelList.add(viewModel);
		}
		dataSourceSelectorView.displayDataSources(viewModelList);
	}

	private void internalHandleException(Throwable caught) {
		// TODO: Add some nice message module in order to show messages from
		// exceptions to user.
		GWT.log("execute*: fail", null);
	}

	private DataSourceDto internalGetDataSourceFromModel(Long id) {
		assert id != null : "Parameter 'id' can not be null";
		for (DataSourceDto dataSource : dataSourceListModel) {
			if (id.equals(dataSource.getId())) {
				return dataSource;
			}
		}
		return null;
	}
}
